/**
 * @projectName: agriculture-smart-systems
 * @author: 小飞
 * @description: TODO
 * @date: 2023/12/20 14:09
 * @version: 1.0
 */
package com.fyt.controller.enclosure;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fyt.controller.utils.Code;
import com.fyt.controller.utils.Result;
import com.fyt.dto.enclosure.EditEnclosureDao;
import com.fyt.dto.enclosure.GetEnclosureDao;
import com.fyt.entity.Enclosure;
import com.fyt.entity.Pasture;
import com.fyt.service.enclosure.EnclosureService;
import com.fyt.service.pasture.PastureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/enclosures/")
public class EnclosureController {
    @Autowired
    private EnclosureService enclosureService;
    @Autowired
    private PastureService pastureService;


    /**
     * @return 返回的牧场列表信息
     */
    @RequestMapping("")
    public Result getAllPasture(){
        QueryWrapper<Pasture> queryWrapper = new QueryWrapper<>();
        //查找出全部为启用的牧场
        queryWrapper.eq("enable",0);
       List<Pasture> pastureList =  pastureService.list(queryWrapper);
       return new Result(pastureList,pastureList!=null? Code.get_ok:Code.get_err,pastureList!=null?"获取成功!":"获取失败!");
    }
    /**
     * 分页查询圈舍信息的控制器
     * @param map 使用map接收前传过来的参数
     * @return Result 返回的数据信息
     * http://localhost:4000/api/enclosures/getEnclosuresPage
     */
    @PostMapping("getEnclosuresPage")
    public Result getEnclosures(@RequestBody Map<String,String> map){
        System.out.println(map);
        return enclosureService.selectEnclosurePage(map);
    }
    /**
     * 修改圈舍的基本信息控制器
     * @param eId 圈舍id
     * @param editEnclosureDao  暂存修改圈舍信息的实体类
     * @return 返回修改返回的状态码以及数据对象
     */
    @PutMapping("updateEnclosure/{eId}")
    public Result getEnclosures(@PathVariable Integer eId,@Valid @RequestBody EditEnclosureDao editEnclosureDao){
        return enclosureService.updateOneEnclosure(eId,editEnclosureDao);
    }

    /**
     * 新增圈舍的控制器
     * @param map 前端传过来的信息
     * @return
     */
    @PostMapping("createEnclosure")
    public Result createEnclosure(@RequestBody Map<String,String> map ){
        return enclosureService.createOneEnclosure(map);
    }
    /**
     * 逻辑删除圈舍(停用/启用)
     * @param eId 圈舍id
     * @param enable 圈舍当前的权限
     * @return Result返回的状态码信息
     */
    @DeleteMapping("deleteEnclosure/{eId}")
    public Result deleteEnclosures(@PathVariable Integer eId, @RequestBody Integer enable){
        return enclosureService.deleteOneEnclosure(eId,enable);
    }
}
